package homework;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
2. 分析以下需求，并用代码实现：
	(1)统计每个单词出现的次数
	(2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
	(3)打印格式：
		to=3
  		think=1
  		you=2
  		//........
 */
public class Test3<entries> {
    public static void main(String[] args) {
        String s = "If you want to change your fate I think you must come to the dark horse to learn java";
        //变成字符数组
        String[] st = s.split(" ");
        Map<String, Integer> map = new HashMap<>();
        for (String s1 : st) {
/*            if(!map.containsKey(s1)){
                map.put(s1,1);
            }else {
                Integer count = map.get(s1);
                count++;
                map.put(s1,count);
            }
        }*/

            map.put(s1, map.containsKey(s1) ? map.get(s1) + 1 : 1);
        }
/*        //设置一个次数
        int count =0;
        //字符数组遍历
        for (String s1 : st) {
            if(map.containsKey(s1)){
                //在原始数组上面加1
                count = map.get(s1) + 1;
            }else {
                count =1;
            }
            map.put(s1,count);
        }*/
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(key + "----" + value);

           }
        }
    }